package SystemeB;

import java.io.PipedReader;
import java.io.PipedWriter;
import java.util.List;

/**************************************************************************************
 ** Class name: CourseFilter Original author: A.J. Lattanze, CMU Date: 12/3/99
 * Version 1.0
 ** 
 ** Adapted by R. Champagne, Ecole de technologie superieure 2002-May-08,
 * 2011-Jan-12
 ** 
 *************************************************************************************** 
 ** Purpose: Assignment 1 for LOG430, Architectures logicielle. This assignment
 * is designed to illustrate a pipe and filter architecture. For the
 * instructions, refer to the assignment write-up.
 ** 
 ** Abstract: This class is intended to be a filter that will key on a particular
 * course number that is provided at instantiation. Note that the stream has to
 * be buffered so that it can be checked to see if the specified course number
 * appears on the steam. If this string appears in the stream from MainFilter,
 * it will be passed to the merge filter for output to a file. If this course
 * string does not appear in the pipe stream, then it is not passed to merge
 * filter.
 ** 
 ** Pseudo Code:
 ** 
 ** connect to input pipe connect to output pipe
 ** 
 ** while not end of line
 ** 
 ** read input pipe
 ** 
 ** if course string specified appears on line of text
 ** 
 ** write line of text to output pipe flush pipe
 ** 
 ** end if
 ** 
 ** end while
 ** 
 ** close pipes
 ** 
 ** Running the program
 ** 
 ** See MainFilter.java
 ** 
 ** Modification Log
 ************************************************************************************** 
 ** 
 **************************************************************************************/

public class MultiCourseFilter extends Thread {

	// Declarations

	boolean Done;

	String[] ListCourseNumberString;
	String allCourseString;

	// Make an input pipe to that will connect to MainFilter filter

	PipedReader InputPipe = new PipedReader();

	PipedWriter OutputPipe = new PipedWriter();

	public MultiCourseFilter(String[] _listCourseNumberString,
			PipedWriter InputPipe, PipedWriter OutputPipe) {

		this.ListCourseNumberString = _listCourseNumberString;

		allCourseString = "";
		for (String courseFilter : ListCourseNumberString) {
			allCourseString += courseFilter + ",";
		}
		allCourseString = allCourseString.substring(0,
				allCourseString.length() - 2);

		try {

			// Connect InputPipe to MainFilter

			this.InputPipe.connect(InputPipe);
			System.out.println("CourseFilter " + allCourseString
					+ ":: connected to upstream filter.");

			// Connect OutputPipe to Merge

			this.OutputPipe = OutputPipe;
			System.out.println("CourseFilter " + allCourseString
					+ ":: connected to downstream filter.");

		} catch (Exception Error) {

			System.out.println("CourseFilter " + allCourseString
					+ ":: Error connecting to other filters.");

		} // try/catch

	} // Constructor

	// This is the method that is called when the thread is started in
	// MainFilter

	public void run() {

		// Declarations

		char[] CharacterValue = new char[1];
		// char array is required to turn char into a string
		String LineOfText = "";
		// string is required to look for the course number
		int IntegerCharacter; // the integer value read from the pipe

		try {

			Done = false;

			while (!Done) {

				IntegerCharacter = InputPipe.read();
				CharacterValue[0] = (char) IntegerCharacter;

				if (IntegerCharacter == -1) { // pipe is closed

					Done = true;

				} else {

					if (IntegerCharacter == '\n') { // end of line

						System.out.println("CourseFilter " + allCourseString
								+ ":: received: " + LineOfText + ".");
						// Check occurence of both course

						for (String courseFilter : ListCourseNumberString) {

							if (LineOfText.indexOf(courseFilter) != -1) {

								System.out.println("CourseFilter "
										+ allCourseString + ":: sending: "
										+ LineOfText + " to output pipe.");
								LineOfText += new String(CharacterValue);
								OutputPipe.write(LineOfText, 0, LineOfText
										.length());
								OutputPipe.flush();

							} // if

						}

						LineOfText = "";

					} else {

						LineOfText += new String(CharacterValue);

					} // if //

				} // if

			} // while

		} catch (Exception Error) {

			System.out.println("CourseFilter::" + allCourseString
					+ " Interrupted.");

		} // try/catch

		try {

			InputPipe.close();
			System.out.println("CourseFilter " + allCourseString
					+ ":: input pipe closed.");

			OutputPipe.close();
			System.out.println("CourseFilter " + allCourseString
					+ ":: output pipe closed.");

		} catch (Exception Error) {

			System.out.println("CourseFilter " + allCourseString
					+ ":: Error closing pipes.");

		} // try/catch

	} // run

} // class
